package com.yc.bbs.dao;

import com.yc.bbs.entity.TblBoard;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * BBS论坛-板块表-13 Mapper 接口
 * </p>
 *
 * @author k
 * @since 2024-11-25
 */
public interface TblBoardMapper extends BaseMapper<TblBoard> {
    @Select("select a.*, topic_cnt, " +
            "       c.publishtime `maxTopic.publishtime`," +
            "       c.title `maxTopic.title`," +
            "       c.cname `maxTopic.user.cname`" +
            "  from tbl_board a" +
            "  left join (select boardid,count(*) topic_cnt" +
            "               from tbl_topic" +
            "              group by boardid) b on a.id=b.boardid" +
            "  left join (select a.publishtime,a.title,b.cname,a.boardid" +
            "               from tbl_topic a" +
            "               join tbl_user b on a.uid=b.id" +
            "              where a.id in (select max(id) maxid" +
            "                               from tbl_topic" +
            "                              group by boardid)" +
            "            ) c on a.id=c.boardid"+
            " where a.parentid=#{pid}")
    @Results(
            id="rm1",
            value = {@Result(column = "id",property = "id",id=true),
                    @Result(column = "parentid",property = "parentid"),
                    @Result(column = "id",property = "children",
                    many=@Many(select = "selectByPid"))
            }
    )
    public List<TblBoard> selectByPid(int pid);
}
